<?php
require('../../config/core_config.php'); // $API_Key is defined in here, keep it constant in all your scripts
if(!defined("API_Key")){die("Direct access not premitted");
if(constant("API_Key")!= $API_Key){die("Dont touch my database you noonga!");

/**
* SourceBans 1.x.x API
* 
* @version 1.0.2
* @Copyright (C) 2012 Jason Bourne
* @ All rights reserved
**/

require('sb_validation.php');

class sb_admins {
/**
 * @desc			Connects to the sourcebans database
 * @return			NULL if connection problem || check $this->dbcon
 * @author			Jason Bourne
 * @since			1.0.0
 */
	public function __construct() // this is executed as soon as a SBF object is initialised
	{
		$this->valid = new sb_validate();
		$this->clear_user();
		include '../../config/databases/sb_database.php'; 
		$dsn = 'mysql:dbname='.$sb_db.';host='.$sb_host;
		
		/*Try connecting to the DB*/
		try {
			$dbh = new PDO($dsn, $sb_user, $sb_pass);
		} catch(PDOException $e) {
			echo $e->getMessage();
			$this->dbcon = NULL;
			return NULL;
		}

		/*Prepare the queries like a boss*/
		$this->check_id = $dbh->prepare("SELECT aid,user,authid,email,immunity,srv_group,srv_flags FROM sb_admins WHERE aid=:id");
		$this->check_email = $dbh->prepare("SELECT aid FROM sb_admins WHERE email=:email");
		$this->check_name = $dbh->prepare("SELECT aid FROM sb_admins WHERE user=:user");
		$this->check_steamid = $dbh->prepare("SELECT aid FROM sb_admins WHERE authid=:steamid");
		$this->check_existid = $dbh->prepare("SELECT aid FROM sb_admins WHERE aid=:id");
		$this->create_user = $dbh->prepare("INSERT INTO sb_admins (user,authid,email) VALUES (:username,:steamid,:email)");
		$this->delete_user = $dbh->prepare("DELETE FROM sb_admins WHERE aid=:id");
		$this->update_user = $dbh->prepare("UPDATE sb_admins SET authid=:steamid,email=:email,immunity=:immunity,srv_group=:group,srv_flags=:flags WHERE aid=:id");
		$this->dbcon = 1;
		return true;
	}	
	
/**
 * @desc			Loads the object with the user's details
 * @param	int	    $id ID number of the user
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */
	public function prepare_user_id($id)
	{	
		$this->clear_user();
		if ($this->dbcon){
			if ($this->valid->id($id)){
				$this->check_id->bindParam(':id', $id);
				$this->check_id->execute();
				$result = $this->check_id->fetchObject();
				$id=$result->aid;
				if (!is_null($id)){
					$this->id=(int)$result->aid;
					$this->username=$result->user;
					$this->steamid=$result->authid;
					$this->email=$result->email;
					$this->immunity=$result->immunity;
					$this->srv_group=$result->srv_group;
					$this->srv_flags=$result->srv_flags;
					return $this->id;
				}
				// ERROR No user by that ID
				return NULL;
			}
			// ERROR Not a valid input for ID
			return NULL;
		}
		//ERROR DATABASE FAILURE
		return NULL;
	}
	
/**
 * @desc			Loads the object with the user's details
 * @param	string	$email EMAIL of the user
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */
	public function prepare_user_email($email)
	{	
		$this->clear_user();
		if ($this->dbcon){
			if ($this->valid->email($email)){
				$this->check_email->bindParam(':email', $email);
				$this->check_email->execute();
				$result = $this->check_email->fetch(PDO::FETCH_OBJ);
				$id=$result->aid;
				if ($id){
					$this->id=(int)$id;
					$this->prepare_user_id($this->id);
					return $this->id;
				}
				// ERROR No user by that EMAIL
				return NULL;
			}
			// ERROR Not a valid input for EMAIL
			return NULL;
		}
		//ERROR DATABASE FAILURE
		return NULL;
	}
	
/**
 * @desc			Loads the object with the user's details
 * @param	string  $name USERBANE of the user
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function prepare_user_name($name)
	{	
		$this->clear_user();
		if ($this->dbcon){
			if ($this->valid->username($name)){
				$this->check_name->bindParam(':user', $name);
				$this->check_name->execute();
				$result = $this->check_name->fetch(PDO::FETCH_OBJ);
				$id=$result->aid;
				if ($id){
					$this->id=(int)$id;
					$this->prepare_user_id($this->id);
					return $this->id;
				}
				// ERROR No user by that NAME
				return NULL;
			}
			// ERROR Invalid Username
			return NULL;
		}
		//ERROR DATABASE FAILURE
		return NULL;
	}
	
/**
 * @desc			Loads the object with the user's details
 * @param	string  $steamid STEAMID of the user
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function prepare_user_steam($steamid)
	{	
		
		$this->clear_user();
		if ($this->dbcon){
			if ($this->valid->steam($steamid)){
				$this->check_steamid->bindParam(':steamid', $steamid);
				$this->check_steamid->execute();
				$result = $this->check_steamid->fetch(PDO::FETCH_OBJ);
				$id=$result->aid;
				if ($id){
					$this->id=(int)$id;
					$this->prepare_user_id($this->id);
					return $this->id;
				}
				// ERROR No user by that STEAMID
				return NULL;
			}
			// ERROR Invalid STEAMID
			return NULL;
		}
		//ERROR DATABASE FAILURE
		return NULL;
	}
	
/**
 * @desc			Retrieve user's ID
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function get_user_id()
	{	
		if (!is_null($this->id)){
			return $this->id;
		}
		//ERROR no user set
		return NULL;
	}
	
/**
 * @desc			Retrieve user's email
 * @return			EMAIL address || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function get_user_email()
	{	
		if (!is_null($this->email)){
			return $this->email;
		}
		//ERROR no user set
		return NULL;
	}
	
/**
 * @desc			Retrieve user's Steam-ID
 * @return			STEAM-ID || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function get_user_steamid()
	{	
		if (!is_null($this->steamid)){
			return $this->steamid;
		}
		//ERROR no user set
		return NULL;
	}
	
/**
 * @desc			Retrieve user's immunity level
 * @return			immunity || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */		
	public function get_user_immunity()
	{	
		if (!is_null($this->immunity)){
			return $this->immunity;
		}
		//ERROR no user set
		return NULL;
	}
	
/**
 * @desc			Retrieve user's permissions group
 * @return			group || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */		
	public function get_user_group()
	{	
		if (!is_null($this->srv_group)){
			return $this->srv_group;
		}
		//ERROR no user set
		return NULL;
	}
	
/**
 * @desc			Retrieve user's flags
 * @return			flags || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */		
	public function get_user_flags()
	{	
		if (!is_null($this->srv_flags)){
			return $this->srv_flags;
		}
		//ERROR no user set
		return NULL;
	}
	
/**
 * @desc			Retrieve user's username
 * @return			USERNAME || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */		
	public function get_user_name()
	{	
		if (!is_null($this->username)){
			return $this->username;
		}
		//ERROR no user set
		return NULL;
	}
	
/**
 * @desc			Clears the object/user details
 * @return			true 
 * @author			Jason Bourne
 * @since			1.0.0
 */		
	public function clear_user()
	{	
		$this->id=NULL;
		$this->username=NULL;
		$this->steamid=NULL;
		$this->email=NULL;
		$this->immunity=NULL;
		$this->srv_group=NULL;
		$this->srv_flags=NULL;
		return true;
	}
	
/**
 * @desc			Create User wooo!
 * @return			ID if user is created // NULL if error
 * @param	string  $steamid STEAMID of the user
 * @param	string  $username USERNAME of the user
 * @param	string  $email EMAIL of the user
 * @param	int     $immunity IMMUNITY of the user
 * @param	string  $group permissions group to place the user in
 * @param	string  $flags additional flags to give the user // abcdfgst
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */		
	public function create_user($username,$steamid,$email,$group=NULL,$immunity=NULL,$flags=NULL)
	{	
		if(!$this->valid->email($email) OR $this->prepare_user_email($email)){
			$this->clear_user();
			return NULL;
		}elseif(!$this->valid->username($username) OR $this->prepare_user_name($username)){
			$this->clear_user();
			return NULL;
		}elseif(!$this->valid->steam($steamid) OR $this->prepare_user_steam($steamid)){
			$this->clear_user();
			return NULL;
		}if(!is_null($group)){
			if(!$this->valid->group($group)){
				return NULL;
			}
		}if(!is_null($immunity)){
			if(!$this->valid->immunity($immunity)){
				return NULL;
			}else{
			}
		}if(!is_null($flags)){
			if(!$this->valid->flags($flags)){
				return NULL;
			}else{
				$flags=$this->sort_string($flags);
			}
		}

		//CREATE THE USER
		$this->create_user->bindParam(':steamid', $steamid);
		$this->create_user->bindParam(':username', $username);
		$this->create_user->bindParam(':email', $email);
		$this->create_user->execute();
		$this->prepare_user_name($username);
		if ($this->id){
			$this->srv_flags=$flags;
			$this->srv_group=$group;
			$this->immunity=$immunity;
			$this->update_user();
			return $this->id;
		}
		return NULL; // if user creation failed somehow
	}

/**
* @desc				Retrieve user's username
* @return			USERNAME || NULL if ERROR
* @author			Jason Bourne
* @since			1.0.0
*/		
	public function delete_user($id=NULL)
	{	
		if (!is_null($id)){
			if ($this->valid->id($id)){
				$this->delete_user->bindParam(':id', $id);
				$this->delete_user->execute();
				$this->clear_user;
				// Remove server access 
				return true;
			}
			return NULL; // Not a valid ID
		}elseif (!is_null($this->id)){
			$id=$this->id;
			$this->delete_user->bindParam(':id', $id);
			$this->delete_user->execute();
			$this->clear_user;
			// Remove server access 
			return true;
		}
		return NULL; 
	}
	
/**
 * @desc			Add a flag to a user
 * @param	string  $flags which flags to add
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function add_user_flags($flags)
	{	
		if (!is_null($this->id) AND $this->valid->flags($flags)){
			$flags=$flags . $this->srv_flags;
			$this->srv_flags=$this->sort_string($flags);
			return $this->id;
		}
		return NULL; // No user loaded
	}

/**
 * @desc			Set a users flags
 * @param	string  $flags User will only have the new flags specifed
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function set_user_flags($flags)
	{	
		if (!is_null($this->id) AND $this->valid->flags($flags)){
			$this->srv_flags=$this->sort_string($flags);
			return $this->id;
		}
		return NULL; // No user loaded
	}

/**
 * @desc			Remove flags from a user
 * @param	string  $flags Which flags to remove from a user
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function remove_user_flags($flags)
	{	
		if (!is_null($this->id) AND $this->valid->flags($flags)){
			$flags=$this->sort_string($flags);
			$flags = str_replace($flags,"", $this->srv_flags);
			$this->srv_flags=$this->sort_string($flags);
			return $this->id;
		}
		return NULL; // No user loaded
	}

/**
 * @desc			Updates the user details in the sb database
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function update_user()
	{	
		if (!is_null($this->id) AND $this->dbcon){
			$this->update_user->bindParam(':id', $this->id);
			$this->update_user->bindParam(':steamid', $this->steamid);
			$this->update_user->bindParam(':email', $this->email);
			$this->update_user->bindParam(':immunity', $this->immunity);
			$this->update_user->bindParam(':group', $this->srv_group);
			$this->update_user->bindParam(':flags', $this->srv_flags);
			$this->update_user->execute();
			return $this->id;
		}
		return NULL; // No user loaded
	}

/**
 * @desc			Change a user's immunity level
 * @param	int     $immunity Between 0 and 99
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function set_user_immunity($immunity)
	{	
		if ($this->valid->immunity($immunity) AND $this->id){
			$this->immunity=$immunity;
			return $this->id;
		}
		//ERROR no user loaded
		return NULL;
	}
	
/**
 * @desc			Change a users permission group
 * @param	string  $group GROUP name 
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function set_user_group($group)
	{	
		if ($this->valid->group($group) AND $this->id){
			$this->srv_group=$group;
			return $this->id;
		}
		//ERROR no user loaded
		return NULL;
	}
	
/**
 * @desc			Used to clean the flag stings // Alphabetises, converts to lowercase, removes duplicates
 * @param	string  $name USERBANE of the user
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */	
	public function sort_string($str) {
		$array = array();
		for($i = 0; $i < strlen($str); $i++) {
			$array[] = $str{$i};
		}
		sort($array);
		$str = implode('', $array);
		$str = strtolower($str);
		return count_chars($str, 3);
	}
}
?>
